from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
from Crypto.Util.Padding import pad, unpad


class AesUtil:

    # 生成随机密钥和IV
    key = b"202cb962ac59075b964b07152d234b70"  # AES-128需要16字节的密钥
    iv = b"202cb962ac590875"   # AES块大小为16字节

    @staticmethod
    def encrypt(data):
        # 创建AES cipher对象
        cipher = AES.new(AesUtil. key, AES.MODE_CBC, AesUtil.iv)
        # 加密前先填充数据以满足块大小的要求
        padded_data = pad(data, AES.block_size)
        # 执行加密
        encrypted_data = cipher.encrypt(padded_data)
        return encrypted_data

    @staticmethod
    def decrypt(encrypted_data):
        # 解密过程
        # 创建一个新的cipher对象用于解密，使用相同的密钥和IV
        decipher = AES.new(AesUtil. key, AES.MODE_CBC, AesUtil.iv)

        # 执行解密并移除填充
        decrypted_padded_data = decipher.decrypt(encrypted_data)
        decrypted_data = unpad(decrypted_padded_data, AES.block_size)
        return decrypted_data

if __name__ == '__main__':
    print(AesUtil.encrypt(b'hello'))